package leetcode.code0948;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int bagOfTokensScore(int[] tokens, int power) {
		Arrays.sort(tokens);
		int ans = 0;
		int l = 0, r = tokens.length - 1;
		while (l <= r) {
			if (power >= tokens[l]) {
				power -= tokens[l++];
				ans += 1;
			} else if (l != r && ans > 0) {
				power += tokens[r--];
				ans -= 1;
			} else {
				l++;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
